package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.entity.Evaluation;
import com.imooc.reader.entity.Member;
import com.imooc.reader.entity.MemberReadState;
import com.imooc.reader.mapper.MemberMapper;
import com.imooc.reader.mapper.MemberReadStateMapper;
import com.imooc.reader.service.Exception.BussinessException;
import com.imooc.reader.service.MemberService;
import com.imooc.reader.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service("memberService")
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private MemberReadStateMapper memberReadStateMapper;

    @Override
    public Member creatMember(String username, String password, String nickname) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        List<Member> list = memberMapper.selectList(queryWrapper);
        //判断该用户是否已经·被注册
        if (list.size() > 0) {
            throw new BussinessException("400", "该用户名已被注册");
        }
        Member member = new Member();
        member.setUsername(username);
        member.setNickname(nickname);
        //对密码进行加密处理
        int salt = new Random().nextInt(1000) + 1000;//创建一个随机的盐值
        String md5password = MD5Utils.md5Digest(password, salt);
        member.setPassword(md5password);
        member.setSalt(salt);
        member.setCreateTime(new Date());
        memberMapper.insert(member);
        return member;
    }

    @Override
    public Member checkLogin(String username, String password) {
        //查询是否有该用户
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("username", username);
        Member member = memberMapper.selectOne(memberQueryWrapper);
        if (member == null) {
            //数据库无此用户
            throw new BussinessException("400", "该用户不存在");
        }
        //对密码进行校验
        String md5 = MD5Utils.md5Digest(password, member.getSalt());
        if (!md5.equals(member.getPassword())) {
            throw new BussinessException("400", "密码错误");
        }
        return member;
    }

    @Override
    public MemberReadState selectStateById(Long memberId, Long bookId) {
        QueryWrapper<MemberReadState> memberReadStateQueryWrapper = new QueryWrapper<>();
        memberReadStateQueryWrapper.eq("memberId", memberId);
        memberReadStateQueryWrapper.eq("bookId", bookId);
        MemberReadState memberReadState = memberReadStateMapper.selectOne(memberReadStateQueryWrapper);

        return memberReadState;
    }

    @Override
    public MemberReadState updateMemberState(Long memberId, Long bookId, Integer readState) {
        //先判断穿来的是否有初始值
        QueryWrapper<MemberReadState> memberReadStateQueryWrapper = new QueryWrapper<MemberReadState>();
        memberReadStateQueryWrapper.eq("member_Id", memberId);
        memberReadStateQueryWrapper.eq("book_Id", bookId);
        MemberReadState memberReadState = memberReadStateMapper.selectOne(memberReadStateQueryWrapper);
        MemberReadState memberReadStates = new MemberReadState();
        if (memberReadState == null) {
            //则初始值为空，需要手动赋值

            memberReadStates.setMemberId(memberId);
            memberReadStates.setBookId(bookId);
            memberReadStates.setCreateTime(new Date());
            memberReadStateMapper.insert(memberReadStates);
        } else {
            //表示有初始值，直接修改
            memberReadStates.setReadState(readState);
            memberReadStateMapper.updateById(memberReadStates);
        }

        return null;
    }

    @Override
    public Evaluation insertScore(Long bookId, Long memberId, Integer score, String content) {
        Evaluation evaluation = new Evaluation();
        evaluation.setBookId(bookId);
        evaluation.setMemberId(memberId);
        //等等设置他的默认数据
        return evaluation;
    }


}
